
<template>
  <el-form ref="form" :model="data" :rules="rules" label-width="100px">
                        <el-form-item label="位置" prop="position">
                    <el-select v-model.trim="data.position" placeholder="请选择位置" clearable  class="filter-item" >
                        <el-option  label="首页" :value="1"></el-option>
                    </el-select>
                    </el-form-item>
                    <el-form-item label="类型">
                         <el-radio-group v-model="data.type">
                            <el-radio :label="0">无跳转</el-radio>
                            <el-radio :label="1">小程序页面</el-radio>
                            <el-radio :label="2">外链</el-radio>
                         </el-radio-group>
                    </el-form-item>
                    <el-form-item label="名称" prop="name">
                        <el-input v-model.trim="data.name" placeholder="请填写名称" />
                    </el-form-item>
                    <el-form-item label="图片" prop="image" >
                        <el-upload
                    :action="$store.state.settings.file_upload_url"
                    :headers ="headerObj"
                    :limit=1
                    list-type="picture-card"
                    :file-list="data.imagefileList"
                    accept="image/png, image/jpeg, image/gif, image/jpg"
                    :on-success="handleimageSuccess"
                    :before-upload="beforeimageUpload"
                    :on-exceed="handleimageExceed"
                    :on-preview="handleimagePreview"
                    :on-remove="handleimageRemove">
                    <i class="el-icon-plus"></i>
                    </el-upload>
                    </el-form-item>
                    <el-form-item label="跳转地址" prop="url">
                        <el-input v-model.trim="data.url" placeholder="请填写跳转地址" />
                    </el-form-item>
                    <el-form-item label="是否显示">
                             <el-switch v-model="data.status"></el-switch>
                    </el-form-item>
                    <el-form-item label="排序" prop="sort">
                        <el-input-number v-model="data.sort" :min="1" :max="10" label="排序"></el-input-number>
                    </el-form-item>

    <el-dialog :visible.sync="dialogVisible" width="30%" append-to-body>
              <img width="100%" :src="previewUrl" alt="">
    </el-dialog>  
    <el-form-item>
      <el-button type="primary" :disabled="$store.state.settings.ajaxButton" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { create } from '@/api/banner'
import { getToken } from '@/utils/auth'


export default {
  name: 'bannerCreate',
  components: { },
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      loading:false,
      dialogVisible:false,
      previewUrl:'',
      headerObj: {
            'Authorization': 'Bearer ' + getToken()
        },
      rules: {
                    position: [ 
                { required: true, message: "请输入位置", trigger: "blur" }
            ], 
            name: [ 
                { required: true, message: "请输入名称", trigger: "blur" }
            ], 
            image: [ 
                { required: true, message: "请输入图片", trigger: "blur" }
            ], 

      },
      data: {
              position:'',
            type:0,
            name:'',
            image:[],
            imagepreviewurl:'',
            imagefileList:[],
            url:'',
            status:true,
            sort:'',

      }
    }
  },
  created() {
  },
  methods: {
    
                handleimageExceed(){ 
                this.$message.error('只能上传一张图片');
             },
            handleimagePreview(file){
                 this.dialogVisible = true;
                 this.previewUrl = file.url;
             },
            handleimageRemove(){
                 this.data.image = '';
             },
             handleimageSuccess(response, file, fileList) {
                this.$store.commit('settings/SET_AJAX_BUTTON', false);
                this.data.image= response.data.src;
            },
             beforeimageUpload(file) {
                this.$store.commit('settings/SET_AJAX_BUTTON', true);
                const isJPG = file.type === "image/jpeg";
                const isPNG = file.type === "image/png";
                const isLt5M = file.size / 1024 / 1024 < 5;
                if (!isJPG && !isPNG) {
                this.$message.error("上传图片只能是 JPG 、PNG格式!");
                }
                if (!isLt5M) {
                this.$message.error("上传图片大小不能超过 2MB!");
                }
                
            },

    
    submit() {
 
      const thiz = this

      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        create({
                       position:this.data.position,
            type:this.data.type,
            name:this.data.name,
            image:this.data.image,
            url:this.data.url,
            status:this.data.status ?1:0,
            sort:this.data.sort,

        }).then(response => {
          this.$store.commit('settings/SET_AJAX_BUTTON', true);
          this.$message({
            message: '添加成功',
            type: 'success',
            duration: 1500,
            onClose() {
              if (thiz.$refs.form !== undefined) {
                thiz.$refs.form.resetFields()
                thiz.data.imagefileList = [];
              }
              thiz.item.dialogVisible = false
            }
          })
        })
      })
    }
  }
}
</script>
